<?php

namespace App\Http\Services;

use App\Http\Model\Hint;
use App\Http\Model\User;
use App\Http\Model\Nameplate;
use App\Http\Model\FinanceUserWallet;
use App\Http\Model\FinanceWealth;
use App\Http\Model\FinanceCharm;

class NameplateServices extends CommonServices
{
    protected $nameplateModel;
    protected $userWalletModel;
    protected $financeWealthModel;
    protected $financeCharmModel;

    public function __construct()
    {
        parent::__construct();

        $this->nameplateModel = new Nameplate();
        $this->userWalletModel = new FinanceUserWallet();
        $this->financeWealthModel = new FinanceWealth();
        $this->financeCharmModel = new FinanceCharm();

    }

    /**
     * 铭牌新增
     */
    public function nameplateAdd($nameplateName, $nameplateImg, $nameplateResource, $nameplateDesc){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];
        $now = time();
        do {
            if(empty($nameplateName) || empty($nameplateImg) || empty($nameplateResource)) {
                $code = Hint::PARAM_ERROR;
                break;
            }

            //新增
            $id = $this->nameplateModel->addNameplate($nameplateName, $nameplateImg, $nameplateResource, $nameplateDesc);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 铭牌编辑
     */
    public function nameplateEdit($nameplateId, $nameplateName, $nameplateImg, $nameplateResource, $nameplateDesc){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];
        $now = time();
        do {
            if(empty($nameplateId)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            $info = $this->nameplateModel->getNameplateById($nameplateId);
            if(empty($info)) {
                $code = Hint::PARAM_ERROR;
                break;
            }

            //更新
            $this->nameplateModel->editNameplate($nameplateId, ['nameplate_name'=>$nameplateName, 'nameplate_img'=>$nameplateImg, 'nameplate_resource'=>$nameplateResource, 'nameplate_desc'=>$nameplateDesc]);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 铭牌用户下发记录新增
     */
    public function nameplateUserAdd($nameplateId, $userId, $timeSt, $timeEt, $sort, $adminId){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];
        $now = time();
        do {
            if(empty($nameplateId) || empty($userId) || empty($timeSt) || empty($timeEt)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            $info = $this->nameplateModel->getNameplateById($nameplateId);
            if(empty($info)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            //是否存在
            $nameplateUser = $this->nameplateModel->getNameplateUser($nameplateId, $userId);
            if(!empty($nameplateUser)) {
                $code = Hint::PARAM_ERROR;
                $msg = '已存在不可新增';
                break;
            }

            //新增
            $id = $this->nameplateModel->addNameplateUser($nameplateId, $userId, $timeSt, $timeEt, $sort, $adminId);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 铭牌用户下发记录编辑
     */
    public function nameplateUserEdit($nameplateId, $userId, $timeSt, $timeEt, $sort, $adminId){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];
        $now = time();
        do {
            if(empty($nameplateId) || empty($userId)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            $info = $this->nameplateModel->getNameplateById($nameplateId);
            if(empty($info)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            //是否存在
            $nameplateUser = $this->nameplateModel->getNameplateUser($nameplateId, $userId);
            if(empty($nameplateUser)) {
                $code = Hint::PARAM_ERROR;
                $msg = '数据已存在,不可重复添加,请求修改数据';
                break;
            }

            //更新
            $this->nameplateModel->editNameplateUser($nameplateId, $userId, ['time_st'=>$timeSt, 'time_et'=>$timeEt, 'sort'=>$sort, 'admin_id'=>$adminId]);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 铭牌用户下发记录删除
     */
    public function nameplateUserDel($nameplateId, $userId, $adminId){
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];
        $now = time();
        do {
            if(empty($nameplateId) || empty($userId)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            //是否存在
            $nameplateUser = $this->nameplateModel->getNameplateUser($nameplateId, $userId);
            if(empty($nameplateUser)) {
                $code = Hint::PARAM_ERROR;
                break;
            }

            //更新
            $this->nameplateModel->editNameplateUser($nameplateId, $userId, ['is_delete'=>1, 'admin_id'=>$adminId]);

        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 根据用户ID获取铭牌数据
     */
    public function getNameplateByUserId($userId){
        $data = [];
        do {
            if(empty($userId)) {
                break;
            }
            $data = $this->nameplateModel->getNameplateByUserId($userId);

            //用户
            $userWallet = $this->userWalletModel->get_wallet($userId);
            $wealthBalance = empty($userWallet['wealth_balance']) ? 0 : $userWallet['wealth_balance'];
            $charmBalance = empty($userWallet['charm_balance']) ? 0 : $userWallet['charm_balance'];

            //财富等级
            $wealthRankList = $this->financeWealthModel->getWealthRankData();
            foreach($wealthRankList as $val) {
                if(empty($val['nameplate_url'])) {
                    continue;
                }
                if($val['min_value']<=$wealthBalance && $wealthBalance<=$val['max_value']) {
                    $item = [
                        'id' => 0,
                        'nameplate_id' => 0,
                        'user_id' => $userId,
                        'sort' => 0,
                        'time_st' => '2024-05-01 00:00:00',
                        'time_et' => '2074-05-01 00:00:00',
                        'nameplate_name' => $val['nameplate_name'],
                        'nameplate_desc' => $val['nameplate_name'],
                        'nameplate_img' => $val['nameplate_url'],
                        'nameplate_resource' => $val['nameplate_url'],
                    ];
                    $data[] = $item;
                    break;
                }
            }

            //魅力等级
            $charmRankList = $this->financeCharmModel->getCharmRankData();
            foreach($charmRankList as $val) {
                if(empty($val['nameplate_url'])) {
                    continue;
                }
                if($val['min_value']<=$charmBalance && $charmBalance<=$val['max_value']) {
                    $item = [
                        'id' => 0,
                        'nameplate_id' => 0,
                        'user_id' => $userId,
                        'sort' => 0,
                        'time_st' => '2024-05-01 00:00:00',
                        'time_et' => '2074-05-01 00:00:00',
                        'nameplate_name' => $val['nameplate_name'],
                        'nameplate_desc' => $val['nameplate_name'],
                        'nameplate_img' => $val['nameplate_url'],
                        'nameplate_resource' => $val['nameplate_url'],
                    ];
                    $data[] = $item;
                    break;
                }
            }
            if(!empty($data)) {
                array_multisort(array_column($data, 'sort'), SORT_ASC, $data);
                $data = array_slice($data, 0, 10);
            }

        } while (false);

        return $data;
    }

    /**
     * 铭牌用户系统下发
     */
    public function nameplateUserSend($nameplateId, $userId, $second, $sort) {
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];
        $now = time();

        do {
            if(empty($nameplateId) || empty($userId) || empty($second)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            $info = $this->nameplateModel->getNameplateById($nameplateId);
            if(empty($info)) {
                $code = Hint::PARAM_ERROR;
                break;
            }
            //是否存在
            $nameplateUser = $this->nameplateModel->getNameplateUser($nameplateId, $userId);
            //新增
            if(empty($nameplateUser)) {
                $timeSt = date('Y-m-d H:i:s', $now);
                $timeEt = date('Y-m-d H:i:s', ($now+$second));
                //新增
                $id = $this->nameplateModel->addNameplateUser($nameplateId, $userId, $timeSt, $timeEt, $sort, 0);
                break;
            }

            //已过期
            if(strtotime($nameplateUser['time_et']) < $now) {
                $timeSt = date('Y-m-d H:i:s', $now);
                $timeEt = date('Y-m-d H:i:s', ($now+$second));
            }else{
                $timeSt = $nameplateUser['time_st'];
                $timeEt = date('Y-m-d H:i:s', (strtotime($nameplateUser['time_et'])+$second));
            }
            //更新
            $this->nameplateModel->editNameplateUser($nameplateId, $userId, ['time_st'=>$timeSt, 'time_et'=>$timeEt, 'sort'=>$sort, 'admin_id'=>0]);

        } while (false);

        return compact('code', 'msg', 'data');
    }

}